const app = getApp()

Page({
  data: {
    itemText: "",
    list: []
  },
  deleteItem(event) {
    const { id } = event.currentTarget.dataset
    wx.showModal({
      title: "警告",
      content: "确定删除此条数据吗？",
      success(res) {
        console.log(res)
        console.log(id)
        // ajax
      }
    })
  },
  toShowPage(event) {
    const { id } = event.currentTarget.dataset
    wx.navigateTo({
      url: "./show?id=" + id
    })
  },
  // async onLoad() {
  async onShow() {
    const self = this
    const res = await app.http({
      url: "https://api.yizhanketang.cn/api/v1/todos",
    })
    this.setData({
      list: res.data.data
    })

    /*app.http({
      url: "https://api.yizhanketang.cn/api/v1/todos",
    }).then(res => {
      console.log(res)
    })*/
  },
  handleClick() {
    const self = this
    if(!this.data.itemText) {
      return
    }
    wx.request({
      method: "POST",
      url: "https://api.yizhanketang.cn/api/v1/todos",
      data: {
        content: this.data.itemText
      },
      success(res) {
        // console.log(res)
        self.setData({
          itemText: ""
        })
        wx.request({
          method: "GET",
          url: "https://api.yizhanketang.cn/api/v1/todos",
          success(res) {
            self.setData({
              list: res.data.data
            })
          }
        })
      }
    })
  },
  handleChange(event) {
    this.setData({
      itemText: event.detail.value
    })
  }
})
